package com.cz.sharding;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cz.entity.ItemEntity;
import com.cz.entity.OrderEntity;
import com.cz.entity.UserEntity;
import com.cz.mapper.ItemMapper;
import com.cz.mapper.OrderMapper;
import com.cz.mapper.UserMapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/shardingall")
public class TestShardingAllController {

    @Autowired
    private ItemMapper itemMapper;


    @Autowired
    private UserMapper userMapper;



    @Autowired
    private OrderMapper orderMapper;
    @GetMapping("/user")
    public void user() {
       for(int i=0;i<10000;i++){
           UserEntity user = new UserEntity();
           user.setName(RandomUtil.randomString("李刚陈华钱胜耿经耳机", 3));
           user.setAge(RandomUtil.randomInt());
           user.setAddress(RandomUtil.randomString("北京上海广州深圳", 3));
           userMapper.insert(user);
       }
    }

    @GetMapping("/item")
    public void item() {
        for(int i=0;i<10000;i++){
            ItemEntity item = new ItemEntity();
            item.setItemName(RandomUtil.randomString("手机电脑花瓶裤子被子",4));
            item.setItemNum(RandomUtil.randomInt());
            itemMapper.insert(item);
        }
    }


    @GetMapping("/order")
    public void order() {
        List<ItemEntity> itemEntityList = itemMapper.selectList(new LambdaQueryWrapper<ItemEntity>());
        List<UserEntity> userEntityList = userMapper.selectList(new LambdaQueryWrapper<UserEntity>());

        for(int i=0;i<10000;i++){
            ItemEntity item = itemEntityList.get(RandomUtil.randomInt(0, itemEntityList.size() - 1));
            OrderEntity order = new OrderEntity();
            UserEntity user = userEntityList.get(RandomUtil.randomInt(0,userEntityList.size()-1));
            System.out.println(user.getId()+"###########"+item.getId());
            order.setUserId(user.getId());
            order.setItemId(item.getId());
            order.setState(RandomUtil.randomInt(0,2));
            orderMapper.insert(order);
        }
    }



    @RequestMapping("/delUser")
    public void delUser(){
        userMapper.delete(new LambdaQueryWrapper<UserEntity>());
    }

    @RequestMapping("/delItem")
    public void delItem(){
        itemMapper.delete(new LambdaQueryWrapper<ItemEntity>());
    }
    @GetMapping("/delorder")
    public void delOrder(){
        orderMapper.delete(new LambdaQueryWrapper<OrderEntity>());
    }



    @RequestMapping("/selectOrder")
    public void selectUser(){
        MPJLambdaWrapper query =  new MPJLambdaWrapper<UserEntity>().selectAll(OrderEntity.class).leftJoin(OrderEntity.class, OrderEntity::getUserId, UserEntity::getId).eq(UserEntity::getId, 1763526831881777156L);
        OrderEntity order = userMapper.selectJoinOne(OrderEntity.class,query);
        System.out.println(JSONUtil.toJsonStr(order));
    }
}
